﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Public;
using BUL;
using System.Net.Mail;


namespace Presentation
{
    public partial class frmAdvertisement : Form
    {
        AdvertisementBUL advertisement = new AdvertisementBUL();
        AdvertedProd advertedProduct = new AdvertedProd(new Product(), new Advertisement());
        public frmAdvertisement()
        {
            InitializeComponent();
        }

        private void frmAdvertisement_Load(object sender, EventArgs e)
        {
            showAllProduct();
        }

        public void showAllProduct()
        {
            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            dgvProAdvert.Columns.Add(chk);
            chk.HeaderText = "Chosen product";
            chk.Name = "chk";
            dgvProAdvert.DataSource = advertisement.getAllProduct();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //get data to save to DB
            int numOfRow = dgvProAdvert.Rows.Count;
            bool check;
            List<string> products = new List<string>();
            List<string> cards = new List<string>();
            LoyaltyCardBUL loyaltyCard = new LoyaltyCardBUL();
            AdvertedCus adCus = new AdvertedCus(new LoyaltyCard(), new Advertisement());
            // create new advertisement id
            advertedProduct.Advertisement.Title = this.txtTitle.Text.Trim();
            advertedProduct.Advertisement.Content = this.txtContent.Text.Trim();
            advertedProduct.Advertisement.FromDate = this.dtpkFrom.Value.ToString("yyyy-MM-dd");
            advertedProduct.Advertisement.ToDate = this.dtpkTo.Value.ToString("yyyy-MM-dd");
            advertisement.saveAdvertisement(advertedProduct);
            // save advetised products
            for (int i = 0; i < numOfRow - 1; i++)
            {
                if (dgvProAdvert[0, i].Value != null)
                {
                    // get checked
                    check = (bool)dgvProAdvert[0, i].Value;
                    if (check)
                    {
                        // add to table advertedProduct
                        advertedProduct.Product.ProductId = (int)dgvProAdvert[1, i].Value;
                        advertedProduct.SaleOff = float.Parse(this.txtSaleOff.Text.Trim());
                        advertisement.saveAdvertedProducts(advertedProduct);
                        // get products to send email
                        products.Add(dgvProAdvert[3, i].Value.ToString());
                    }
                }
            }
            // get eac email of customer
            DataTable lcDt = loyaltyCard.LoadAll();
            //int numOfCards = lcDt.Rows.Count();
            if (lcDt.Rows.Count > 0)
            {
                for (int i = 0; i < lcDt.Rows.Count; i++)
                {
                    // your index is in i
                    adCus.LoyaltyCard.CardId = int.Parse(lcDt.Rows[i][0].ToString());
                    loyaltyCard.saveAdvertedCus(adCus);
                    cards.Add(lcDt.Rows[i][6].ToString());
                }

            }
            // send email to subcribed customers
            if (cards.Count > 0)
            {
                for (int i = 0; i < cards.Count; i++)
                {
                    try
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress("conveniencestore9@gmail.com");
                        mail.To.Add(cards[i]);
                        mail.Subject = advertedProduct.Advertisement.Title;
                        mail.Body = advertedProduct.Advertisement.Content + System.Environment.NewLine;
                        foreach (string product in products) // Loop through List with foreach
                        {
                            mail.Body += "- " + product + System.Environment.NewLine;
                        }
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential("conveniencestore9@gmail.com", "sadgroup");
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                        MessageBox.Show("Send mail successfully");
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void frmAdvertisement_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmMain main = new frmMain();
                this.Hide();
                main.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtContent.Clear();
            this.txtSaleOff.Clear();
            this.txtTitle.Clear();
            this.dtpkFrom.ResetText();
            this.dtpkTo.ResetText();
            showAllProduct();
        }
    }
}
